Completed
Push — master ( 2281e8...8b163e )
by Justin
01:35
created

NameForm.js ➔ NameForm   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 9.2
1
var ExtensibleData = require('./ExtensibleData'),
2
    NamePart = require('./NamePart'),
3
    utils = require('./utils');
4
5
/**
6
 * A form of a name.
7
 * 
8
 * @constructor
9
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
10
 */
11
var NameForm = function(json){
12
  
13
  // Protect against forgetting the new keyword when calling the constructor
14
  if(!(this instanceof NameForm)){
15
    return new NameForm(json);
16
  }
17
  
18
  // If the given object is already an instance then just return it. DON'T copy it.
19
  if(NameForm.isInstance(json)){
20
    return json;
21
  }
22
  
23
  ExtensibleData.call(this, json);
24
  
25
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
26
    this.setLang(json.lang);
27
    this.setFullText(json.fullText);
28
    this.setParts(json.parts);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
29
  }
30
};
31
32
NameForm.prototype = Object.create(ExtensibleData.prototype);
33
34
NameForm._gedxClass = NameForm.prototype._gedxClass = 'GedcomX.NameForm';
35
36
/**
37
 * Check whether the given object is an instance of this class.
38
 * 
39
 * @param {Object} obj
40
 * @returns {Boolean}
41
 */
42
NameForm.isInstance = function(obj){
43
  return utils.isInstance(obj, this._gedxClass);
44
};
45
46
/**
47
 * Get the lang tag
48
 * 
49
 * @returns {String} lang
50
 */
51
NameForm.prototype.getLang = function(){
52
  return this.lang;
53
};
54
55
/**
56
 * Set the lang tag
57
 * 
58
 * @param {String} lang
59
 * @returns {NameForm} This instance
60
 */
61
NameForm.prototype.setLang = function(lang){
62
  this.lang = lang;
63
  return this;
64
};
65
66
/**
67
 * Get the full text
68
 * 
69
 * @returns {String} fullText
70
 */
71
NameForm.prototype.getFullText = function(){
72
  return this.fullText;
73
};
74
75
/**
76
 * Set the full text
77
 * 
78
 * @param {String} fullText
79
 * @returns {NameForm} This instance
80
 */
81
NameForm.prototype.setFullText = function(fullText){
82
  this.fullText = fullText;
83
  return this;
84
};
85
86
/**
87
 * Get the name parts
88
 * 
89
 * @returns {NamePart[]}
90
 */
91
NameForm.prototype.getParts = function(){
92
  return this.parts || [];
93
};
94
95
/**
96
 * Set the name parts
97
 * 
98
 * @param {NamePart[]|Object[]} parts
99
 * @returns {NameForm} This instance
100
 */
101
NameForm.prototype.setParts = function(parts){
102
  if(Array.isArray(parts)){
103
    var nameForm = this;
104
    nameForm.parts = [];
105
    parts.forEach(function(part){
106
      nameForm.addPart(part);
107
    });
108
  }
109
  return this;
110
};
111
112
/**
113
 * Add a name part
114
 * 
115
 * @param {NamePart|Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
116
 * @returns {NameForm}
117
 */
118
NameForm.prototype.addPart = function(part){
119
  if(part){
120
    if(!Array.isArray(this.parts)){
121
      this.parts = [];
122
    }
123
    this.parts.push(NamePart(part));
124
  }
125
  return this;
126
};
127
128
/**
129
 * Export the object as JSON
130
 * 
131
 * @return {Object} JSON object
132
 */
133
NameForm.prototype.toJSON = function(){
134
  var json = ExtensibleData.prototype.toJSON.call(this);
135
  
136
  if(this.lang){
137
    json.lang = this.lang;
138
  }
139
  
140
  if(this.fullText){
141
    json.fullText = this.fullText;
142
  }
143
  
144
  if(this.parts){
145
    json.parts = this.parts.map(function(p){
146
      return p.toJSON();
147
    });
148
  }
149
  
150
  return json;
151
};
152
153
module.exports = NameForm;